package com.elsa.common.handler;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import com.alibaba.fastjson.JSONObject;
import com.elsa.common.entity.ElsaResponse;
import com.elsa.common.utils.ElsaUtil;

/**
 * @projectName：elsa-common
 * @typeName：ElsaAuthExceptionEntryPoint
 * @author：cos
 * @date：2021年5月11日
 * @version
 * @desc：资源服务器异常翻译器：令牌不正确返回401
 */
public class ElsaAuthExceptionEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        ElsaResponse elsaResponse = new ElsaResponse();
        ElsaUtil.makeResponse(
                response, MediaType.APPLICATION_JSON_UTF8_VALUE,
                HttpServletResponse.SC_UNAUTHORIZED, elsaResponse.message("token无效")
        );
    }
}
